<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ MCP SDK/MCP Server SDK  Tools Development</title>
        <meta name="description" content="A lightweight Java 8 Model Context Protocol (MCP) SDK. MCP Server SDK  Tools Development"/>
        <meta name="keywords" content="mcp, mcp sdk, java mcp, mcp client, mcp server, java8 mcp, Tools Development"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="/asset/style/main.css"/>
        <link rel="icon" type="image/png" href="/asset/favicon.ico"/>
        <script src="/asset/common.js"></script>
        <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@200..900&family=Noto+Serif+TC:wght@200..900&family=Noto+Serif:ital,wght@0,100..900;1,100..900&family=Open+Sans:ital,wght@0,300..800;1,300..800&display=swap" rel="stylesheet" />
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 Home</a>
                    | ⚙️ Source:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-mcp">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-mcp">Gitcode</a>
                    |
                    <a href="/cn">Chinese Version</a>
                </div>
                <h1>AJ MCP SDK</h1>
                <h3>User Manual</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/">Home</a>
                    </li>
                    <li>
                        <a href="/architecture/index">Architecture</a>
                    </li>
                </ul>
                <h3>MCP Client SDK</h3>
                <ul>
                    <li>
                        <a href="/client/setup-client">Client SDK Setup</a>
                    </li>
                    <li>
                       <a href="/client/initialization">Initialization</a>
                    </li>
                    <li>
                       <a href="/client/resource">Resources Usage</a>
                    </li>
                     <li>
                       <a href="/client/prompt">Prompts Usage</a>
                    </li>
                     <li>
                       <a href="/client/tool">Tools Usage</a>
                    </li>
                </ul>
                <h3>MCP Server SDK</h3>
                <ul>
                      <li>
                           <a href="/server/usage-server">Server SDK Usage</a>
                      </li>
                      <li>
                           <a href="/server/res-server">Resources Usage</a>
                      </li>
                      <li>
                           <a href="/server/pro-server">Prompts Usage</a>
                      </li>
                      <li>
                             <a href="/server/tool-server">Tools Usage</a>
                       </li>
                      <li>
                         <a href="/server/sample">Integration Samples</a>
                      </li>
                </ul>

                <h3>Misc</h3>
                <ul>
                    <li><a href="/misc/versions">Release History</a></li>
                    <li><a href="/misc/contact">Contact</a></li>
                </ul>
            </menu>
            <article>
                <h1>Tools Development</h1>
<p>Tools are created by annotating methods with <code>@Tool</code> in classes marked with <code>@McpService</code>. The framework automatically discovers these methods and
exposes them as callable tools.</p>
<pre><code class="language-java">
@McpService
public class MyTools {
    @Tool(description = &quot;Echoes a string&quot;)
    public String echoString(@ToolArg(description = &quot;The string to be echoed&quot;) String input) {
        return input;
    }

    @Tool(&quot;customName&quot;, description = &quot;Tool with custom name&quot;)
    public String myMethod() {
        return &quot;result&quot;;
    }
}
</code></pre>
<h2>Parameter Handling and Validation</h2>
<p>Tool parameters are annotated with @ToolArg to provide metadata for validation and schema generation. Each parameter can specify a name, description, and whether it's required.</p>
<h3>Parameter Annotation</h3>
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Description</th>
<th>Default</th>
</tr>
</thead>
<tbody>
<tr>
<td>value</td>
<td>Parameter name (uses method parameter name if empty)</td>
<td>Empty string</td>
</tr>
<tr>
<td>description</td>
<td>Parameter description for clients</td>
<td>Empty string</td>
</tr>
<tr>
<td>required</td>
<td>Whether parameter is mandatory</td>
<td>true</td>
</tr>
</tbody>
</table>
<h2>Type Mapping</h2>
<p>The framework automatically maps Java types to JSON Schema types:</p>
<table>
<thead>
<tr>
<th>Java Type</th>
<th>JSON Schema Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>String, Character</td>
<td>&quot;string&quot;</td>
</tr>
<tr>
<td>int, long, float, double, Number</td>
<td>&quot;number&quot;</td>
</tr>
<tr>
<td>boolean, Boolean</td>
<td>&quot;boolean&quot;</td>
</tr>
<tr>
<td>Other types</td>
<td>&quot;Object&quot;</td>
</tr>
</tbody>
</table>
<h2>Example Tool Implementations</h2>
<h3>Basic String Tool</h3>
<pre><code class="language-java">@Tool(description = &quot;Echoes a string&quot;)
public String echoString(@ToolArg(description = &quot;The string to be echoed&quot;) String input) {
    return input;
}
</code></pre>
<h3>Parameterless Tool</h3>
<pre><code class="language-java">@Tool(description = &quot;Takes 10 seconds to complete&quot;)
public String longOperation() throws Exception {
    TimeUnit.SECONDS.sleep(5);
    return &quot;ok&quot;;
}
</code></pre>
<h3>Content Return Tool</h3>
<pre><code class="language-java">@Tool(description = &quot;A nice pic&quot;)
public ContentImage image() {
    String base64EncodedImage = ServerUtils.encodeImageToBase64(&quot;bird-eye.jpg&quot;);
    ContentImage content = new ContentImage();
    content.setMimeType(&quot;image/jpg&quot;);
    content.setData(base64EncodedImage);
    return content;
}
</code></pre>
<h3>Multi-Content Tool</h3>
<pre><code class="language-java">@Tool(description = &quot;List ALL&quot;)
public List&lt;Content&gt; getAll() {
    List&lt;Content&gt; list = new ArrayList&lt;&gt;();
    list.add(new ContentImage(/* ... */));
    list.add(new ContentText(&quot;Hello World&quot;));
    return list;
}
</code></pre>

            </article>

            <div></div>
        </div>
        <footer>
            AJ MCP SDK, a part of
            <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a>
            open source. Mail:frank@ajaxjs.com, visit
            <a href="https://blog.csdn.net/zhangxin09" target="_blank">my blog(In Chinese)</a>. <br/> <br/> Copyright © 2025 Frank Cheung. All rights reserved.
        </footer>
    </body>
</html>